package com.example.composableexample.login

import androidx.compose.animation.animateColor
import androidx.compose.animation.core.animateDp
import androidx.compose.animation.core.tween
import androidx.compose.animation.core.updateTransition
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Card
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.material.Icon
import androidx.compose.material.MaterialTheme
import androidx.compose.material.OutlinedTextField
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material.TextFieldColors
import androidx.compose.material.TextFieldDefaults
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Email
import androidx.compose.material.icons.filled.Lock
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.composableexample.R

@Preview
@Composable
fun LoginSimple1Preview() {
    Surface(color = MaterialTheme.colors.background) {
        Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
            Card(
                elevation = 8.dp,
                shape = MaterialTheme.shapes.medium
            ) {
                Column(
                    modifier = Modifier.padding(16.dp),
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.Center
                ) {
                    val emailText = remember { mutableStateOf("") }
                    val passwordText = remember { mutableStateOf("") }
                    val colors = TextFieldDefaults.outlinedTextFieldColors(
                        focusedBorderColor = MaterialTheme.colors.primary,
                        unfocusedBorderColor = MaterialTheme.colors.onSurface,
                        cursorColor = MaterialTheme.colors.onPrimary
                    )
                    Image(
                        modifier = Modifier.size(100.dp),
                        painter = painterResource(id = R.drawable.jetpack),
                        contentDescription = "logo"
                    )
                    Spacer(modifier = Modifier.height(16.dp))
                    Text(
                        text = "邮箱:",
                        color = Color.Gray,
                        modifier = Modifier.fillMaxWidth(0.85f),
                        textAlign = TextAlign.Left
                    )
                    MyTextField(
                        value = emailText.value,
                        colors = colors,
                        trailingIcon = Icons.Default.Email,
                        modifier = Modifier.fillMaxWidth(0.85f),
                        trailingTintColor = MaterialTheme.colors.primary,
                        keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Email),
                        onValueChange = {
                            emailText.value = it
                        }
                    )
                    Spacer(modifier = Modifier.height(8.dp))
                    Text(
                        text = "密码:",
                        color = Color.Gray,
                        modifier = Modifier.fillMaxWidth(0.85f),
                        textAlign = TextAlign.Left
                    )
                    MyTextField(
                        value = passwordText.value,
                        colors = colors,
                        trailingIcon = Icons.Default.Lock,
                        modifier = Modifier.fillMaxWidth(0.85f),
                        trailingTintColor = MaterialTheme.colors.primary,
                        keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
                        visualTransformation = PasswordVisualTransformation(),
                        onValueChange = {
                            passwordText.value = it
                        }
                    )

                    Spacer(modifier = Modifier.height(16.dp))
//                    Button(
//                        onClick = { /*TODO*/ },
//                        colors = ButtonDefaults.buttonColors(),
//                        modifier = Modifier
//                            .fillMaxWidth(0.8f)
//                            .height(46.dp),
//                        enabled = emailText.value.isNotBlank() && passwordText.value.isNotBlank(),
//                        shape = MaterialTheme.shapes.medium
//                    ) {
//                        Text(text = "登录", color = Color.White)
//                    }
                    AnimatedButton(emailText, passwordText)

                    Spacer(modifier = Modifier.height(16.dp))

                    Text(text = "忘记密码", fontSize = 12.sp, color = Color.Blue)
                }
            }
        }
    }
}

@Composable
fun MyTextField(
    value: String,
    colors: TextFieldColors,
    trailingIcon: ImageVector,
    trailingTintColor: Color,
    modifier: Modifier = Modifier,
    keyboardOptions: KeyboardOptions,
    visualTransformation: VisualTransformation = VisualTransformation.None,
    onValueChange: (String) -> Unit,
) {
    OutlinedTextField(
        value = value,
        onValueChange = onValueChange,
        modifier = modifier,
        colors = colors,
        trailingIcon = {
            Icon(
                imageVector = trailingIcon,
                contentDescription = "",
                tint = trailingTintColor
            )
        },
        keyboardOptions = keyboardOptions,
        visualTransformation = visualTransformation,
        singleLine = true
    )
}

enum class ButtonState {
    Normal, Pressed
}

@Composable
fun AnimatedButton(emailText: MutableState<String>, passwordText: MutableState<String>) {
    val buttonState = remember {
        mutableStateOf(ButtonState.Normal)
    }
    val transition = updateTransition(targetState = buttonState, label = "buttonTransition")
    val duration = 600
    val buttonBackgroundColor by transition.animateColor(
        transitionSpec = { tween(durationMillis = duration) },
        label = "buttonBackgroundColor"
    ) { state ->
        when (state.value) {
            ButtonState.Normal -> Color.Blue
            ButtonState.Pressed -> Color.Gray
        }
    }
    val buttonWidth by transition.animateDp(
        transitionSpec = { tween(durationMillis = duration) },
        label = "buttonWidth"
    ) { state ->
        when (state.value) {
            ButtonState.Normal -> 320.dp
            ButtonState.Pressed -> 60.dp
        }
    }

    val buttonShape by transition.animateDp(
        transitionSpec = { tween(durationMillis = duration) },
        label = "buttonShape"
    ) { state ->
        when (state.value) {
            ButtonState.Normal -> 4.dp
            ButtonState.Pressed -> 100.dp
        }
    }

    Button(
        modifier = Modifier.width(buttonWidth).height(46.dp),
        shape = RoundedCornerShape(buttonShape),
        enabled = emailText.value.isNotBlank() && passwordText.value.isNotBlank(),
        colors = ButtonDefaults.buttonColors(
            backgroundColor = buttonBackgroundColor,
            disabledBackgroundColor = Color.Blue.copy(0.5f)
        ),
        onClick = {
            buttonState.value = if (buttonState.value == ButtonState.Normal) {
                ButtonState.Pressed
            } else {
                ButtonState.Normal
            }
        }
    ) {
        if (buttonState.value == ButtonState.Normal) {
            Text(text = "登录", color = Color.White)
        } else {
            CircularProgressIndicator(color = Color.White,
                strokeWidth = 2.dp,
                modifier = Modifier.size(24.dp, 24.dp))
        }
    }
}